package com.dao.impl;

import com.dao.OrderdetailDao;
import com.dao.OrderdetailForOrderinfoDao;
import com.entity.Orderdetail;
import com.entity.OrderdetailExample;
import com.mapper.OrderdetailMapper;
import com.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class OrderdetailDaoImpl implements OrderdetailDao, OrderdetailForOrderinfoDao {
    @Resource
    OrderdetailMapper orderdetailMapper;

    /**
     * 根据订单详情编号查询一个订单详情
     * @param odid  订单详情编号
     * @return 订单详情对象
     */
    public Orderdetail getByOdid(String odid){
        return orderdetailMapper.selectByPrimaryKey(odid);
    }
//
    
    //根据订单编号查询订单详情集合
    public List<Orderdetail> getByOrid(String orid){
        return orderdetailMapper.selectByOrid(orid);
    }

    public List<Orderdetail> getAllByPage(PageInfo pageInfo){
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) orderdetailMapper.countByExample(new OrderdetailExample()));
        return orderdetailMapper.getAllByPage(map);
    }

    @Override
    public List<Orderdetail> searchNameByPage(PageInfo pageInfo,String adname) {

        OrderdetailExample orderdetailExample = new OrderdetailExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        orderdetailExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        orderdetailExample.setLimit(limit);
        //createCriteria 创建一个条件查询
        //orderdetailExample.createCriteria().andAdnameLike(adname);
        return orderdetailMapper.selectByExample(orderdetailExample);
    }

    /**
     * 根据主键查询
     * @param orid
     * @return
     */
    public Orderdetail getOrderdetailByOrid(String orid){
        return orderdetailMapper.selectByPrimaryKey(orid);
    }

    /**
     * 添加一个实体
     * @param orderdetail
     * @return
     */
    public int addOrderdetail(Orderdetail orderdetail){
        return orderdetailMapper.insertSelective(orderdetail);
    }
    /**
     * 修改实体,如果有条件 需要设置example
     */
    public int updateOrderdetail(Orderdetail orderdetail){
        return orderdetailMapper.updateByPrimaryKeySelective(orderdetail);
    }
    public int updateOrderdetailByExample(Orderdetail orderdetail){
        return orderdetailMapper.updateByExample(orderdetail,null);
    }

    /**
     * 删除一个实体
     * @param odid
     * @return
     */
    public int deleteOrderdetailByPrimayKey(String odid){
        return orderdetailMapper.deleteByPrimaryKey(odid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public int deleteOrderdetailByExample(){
        return orderdetailMapper.deleteByExample(null);
    }


}
